package com.itheima.JDK8date;

import java.util.Scanner;

public class TryCatchDemo {
    public static void main(String[] args) {
        Student s = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("亲输入学生姓名");
        String name = sc.next();
        s.setName(name);
        while (true) {
            System.out.println("亲输入学生年龄");
            String strAge = sc.nextLine();
            try {
            int age = Integer.parseInt(strAge);
                s.setAge(age);
                break;
            } catch (NumberFormatException a) {
                System.out.println("请输入整数");
                //continue;
            } catch (RuntimeException b) {
                System.out.println("请输入一个符合范围的");
                continue;
            }
        }
        System.out.println(s);
    }
}

//学生类
class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >= 18 && age <= 25) {
            this.age = age;
        } else {
            throw new RuntimeException("年龄超出范围!");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}